package com.changhai.diabetes.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.changhai.diabetes.model.InterGroupUser;
import com.changhai.diabetes.model.QueryDTO;
import com.changhai.diabetes.model.ResearchGroup;
import com.changhai.diabetes.model.SessionUser;
import com.changhai.diabetes.model.TableForPatient;
import com.changhai.diabetes.model.User;
import com.changhai.diabetes.model.page.Page;
import com.changhai.diabetes.model.page.Pagination;
import com.changhai.diabetes.service.GroupService;
import com.changhai.diabetes.util.Constants;
import com.opensymphony.xwork2.ActionContext;

/**
 * 科研分组信息
 * 
 * @author Andy
 * 
 */
public class GroupServiceImpl extends CommonServiceImpl implements GroupService {

	public Page<?> searchQuery(QueryDTO queryDTO) {
		List<Map> list = new ArrayList();
		List<ResearchGroup> result = new ArrayList<ResearchGroup>();

		SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
		if (sUser != null) {
			if (sUser.getAdminType() == 0) {
				queryDTO.getConditions().put("userId", sUser.getId());
			}
			list = this.getList(Constants.DS_CHANGHAI, "researchGroup.searchQuery", queryDTO.getConditions());
		}

		if (list != null && list.size() > 0) {
			try {
				for (Iterator iter = list.iterator(); iter.hasNext();) {
					ResearchGroup g = (ResearchGroup) iter.next();
					if (g.getParentgroup() != null && g.getParentgroup() > 0) {
						ResearchGroup father = this.searchGroupById(g.getParentgroup());
						if (father != null) {
							g.setGroupname(g.getGroupbh());
							g.setGroupbh(father.getGroupbh());
						}
					}
					result.add(g);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return new Pagination((Integer) this.getObject(Constants.DS_CHANGHAI, "researchGroup.countQuery",
				queryDTO.getConditions()), result, queryDTO.getConditions());
	}

	public List<ResearchGroup> getGroupList() {
		List<ResearchGroup> list = new ArrayList<ResearchGroup>();
		SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
		if (sUser != null) {
			Map<String, Number> map = new HashMap<String, Number>();
			if (sUser.getAdminType() == 0) {
				map.put("userId", sUser.getId());
			}
			list = this.getList(Constants.DS_CHANGHAI, "researchGroup.searchQuery", map);
		}

		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				ResearchGroup g = (ResearchGroup) list.get(i);
				if (g.getParentgroup() != null && g.getParentgroup() > 0) {
					ResearchGroup father = this.searchGroupById(g.getParentgroup());
					if (father != null) {
						g.setGroupname(g.getGroupbh());
						g.setGroupbh(father.getGroupbh());
						list.set(i, g);
					}
				}
			}
		}
		return list;
	}

	public ResearchGroup searchGroupById(Long researchgroupid) {
		ResearchGroup group = null;
		group = (ResearchGroup) this.getObject(Constants.DS_CHANGHAI, "researchGroup.getByGroupId", researchgroupid);
		return group;
	}

	public List<ResearchGroup> searchSubGroupbyParentId(Long researchgroupid) {
		List<ResearchGroup> list = new ArrayList<ResearchGroup>();
		if (researchgroupid != null) {
			list = this.getList(Constants.DS_CHANGHAI, "researchGroup.getGanyuGroupListById", researchgroupid);
		}
		return list;
	}

	public void saveEditGroup(ResearchGroup researchGroup, String[] groupbh, String[] groupname) throws Exception {
		if (researchGroup != null) {
			this.updateObject(Constants.DS_CHANGHAI, "researchGroup.updateByPrimaryKey", researchGroup);
		}
		if (groupbh != null && groupname != null && groupbh.length > 0 && groupname.length > 0) {
			for (int i = 0; i < groupbh.length; i++) {
				ResearchGroup g = new ResearchGroup();
				g.setGroupstatus(0);
				g.setGroupbh(groupbh[i]);
				g.setGroupname(groupname[i]);
				g.setParentgroup(researchGroup.getResearchgroupid());
				this.insertObjectID(Constants.DS_CHANGHAI, "researchGroup.insert", g);
			}
		}
	}

	public Long saveNewGroup(ResearchGroup researchGroup) throws Exception {
		Integer researchgroupid = 0;
		if (researchGroup != null) {
			researchGroup.setParentgroup(new Long(0));// 默认为父组
			researchGroup.setGroupstatus(0);// 新增的课题组默认状态为进行中
			researchGroup.setPatientcount(0);
			researchgroupid = this.insertObjectID(Constants.DS_CHANGHAI, "researchGroup.insert", researchGroup);

			SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
			if (researchgroupid > 0) {
				InterGroupUser interGroupUser = new InterGroupUser();
				interGroupUser.setResearchgroupid(new Long(researchgroupid));
				interGroupUser.setUserid(sUser.getId());
				this.insertObjectID(Constants.DS_CHANGHAI, "interGroupUser.insert", interGroupUser);
			}
		}
		return new Long(researchgroupid);
	}

	public List<TableForPatient> searchTables() {
		List<TableForPatient> list = new ArrayList<TableForPatient>();
		list = this.getList(Constants.DS_CHANGHAI, "tableForPatient.getAllTbInfo", null);
		return list;
	}

	public void saveEditHospital(User user) {
		if (user != null && user.getUserid() != null) {
			this.updateObject(Constants.DS_CHANGHAI, "user.updateInfoByPrimaryKey", user);
		}
	}
}
